﻿using System;
using System.Drawing;
using System.ComponentModel;
using DevExpress.XtraEditors.Controls;

namespace ProgramMain.Framework
{
    /// <summary>
    /// Implementación de un botón de estado (StateButton)
    /// que se mantiene pulsado hasta que se vuelve a pulsar.
    /// </summary>
    [ToolboxItem(true)]
    //[ToolboxBitmap(typeof(StateButton), "StateButton.bmp")]
    public class StateButton : DevExpress.XtraEditors.SimpleButton
    {
        private bool downState = false;

        /// <summary>
        /// Constructor por defecto de un StateButton
        /// </summary>
        public StateButton() : base()
        {
        }

        /// <summary>
        /// Estado actual del botón.
        /// </summary>
        [DefaultValue(false), Category("Behavior"), Description("Pushed state of the button.")]
        public bool Down
        {
            get
            {
                return downState;
            }
            set
            {
                if (downState != value)
                {
                    downState = value;
                    UpdatePressedState(downState);
                    if (downState)
                    {
                        ButtonStyle = BorderStyles.UltraFlat;
                    }
                    else
                    {
                        ButtonStyle = BorderStyles.Default;
                    }
                    if (DownStateValueChanged != null)
                        DownStateValueChanged(this, new DownStateArgs(downState));
                }
            }
        }

        public class DownStateArgs : EventArgs
        {
            private bool _Down;
            public bool Down
            {
                get
                {
                    return _Down;
                }
            }
            
            public DownStateArgs(bool bDown)
            {
                _Down = bDown;    
            }
        }
        public event EventHandler<DownStateArgs> DownStateValueChanged;


        /// <summary>
        /// Change state on mouse event
        /// </summary>
        /// <param name="e">Mouse Event Parameters</param>
        protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
        {
            base.OnMouseUp(e);
            Down = !Down;
        }

        /// <summary>
        /// Change state on key event
        /// </summary>
        /// <param name="e">Keyboard Event Parameters</param>
        protected override void OnKeyUp(System.Windows.Forms.KeyEventArgs e)
        {
            base.OnKeyUp(e);
            if(e.KeyData == System.Windows.Forms.Keys.Space)
                Down = !Down;
        }
   }
}
